<?php
namespace app\live\controller;

use helper\JwtHandler;
use think\facade\Cookie;
use traits\controller\JumpTraits;
use traits\controller\ListTraits;
use traits\controller\ViewTraits;

/**
 * 控制器基础类
 */
abstract class BaseController
{

  use JumpTraits;
  use ListTraits;
  use ViewTraits;

  /**
   * Request实例
   * @var \think\Request
   */
  protected $request;

  /**
   * 应用实例
   * @var \think\App
   */
  protected $app;

  /**
   * 控制器中间件
   * @var array
   */
  protected $middleware = [];

  /**
   * 构造方法
   * @access public
   */
  public function __construct()
  {
    $this->app       = app();
    $this->request   = $this->app->request;
  }

  /**
   * 获取主播登录信息
   * @return array
   * @throws \Exception
   */
  public function getRoomLogin(){
    if(!Cookie::has('anchor_login')){
      if($this->request->isAjax()){
        $this->error('当前账号未登录');
      }else{
        $this->redirect('public/wrong',['msg'=>'登录超时']);
      }
    }
    $secret = config('cloud.tencent.secretKey');
    $token  = Cookie::get('anchor_login');
    return JwtHandler::decode($token, $secret);
  }

  /**
   * 获取会员登录信息
   * @return array
   * @throws \Exception
   */
  public function getMemberLogin(){
    try{
      if(!Cookie::has('member_login')) return [];
      $secret = config('cloud.tencent.secretKey');
      $token  = Cookie::get('member_login');
      return JwtHandler::decode($token, $secret);
    } catch (\Exception $e){
      return [];
    }
  }

  /**
   * 检测会员登录
   * @return bool
   * @throws \Exception
   */
  public function checkMemberLogin(){
    if(empty($this->getMemberLogin())) return false;
    return true;
  }

}
